/****************************************************
    文件：UIBottomBarItem.cs
    作者：L.S.Xing
    邮箱: 170105971@qq.com
    日期：#CreateTime#
    功能：
*****************************************************/

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace LSX
{
    public class UIBottomBarItem : MonoBehaviour, IPointerDownHandler
    {
        [Tooltip("激活时显示的背景图片")]
        [SerializeField] Image backgroundImage;
        [Tooltip("激活时显示的内容")]
        GameObject content;
        [SerializeField] Text title;

        //当前的索引值
        int index;


        private void OnEnable()
        {
            UIBottomBar.Instance.OnBottomBarItemSelected += OnSelected;
            UIBottomBar.Instance.OnBottomBarItemDeselected += OnDeselected;
        }

        private void OnDestroy()
        {
            UIBottomBar.Instance.OnBottomBarItemSelected -= OnSelected;
            UIBottomBar.Instance.OnBottomBarItemDeselected -= OnDeselected;
        }

        /// <summary>
        /// 初始化Bottom Item
        /// </summary>
        /// <param name="index">当前的索引值</param>
        /// <param name="content">要显示的内容</param>
        /// <param name="title">标题</param>
        public void Init(int index, GameObject content, string title)
        {
            this.index = index;
            this.content = content;
            this.title.text = title;

            backgroundImage.enabled = false;
            content.SetActive(false);
        }

        void OnSelected(int selectedIndex)
        {
            if (selectedIndex == index)
            {
                backgroundImage.enabled = true;
                content.SetActive(true);
            }
        }

        void OnDeselected(int DeselectedIndex)
        {
            if (DeselectedIndex == index)
            {
                backgroundImage.enabled = false;
                content.SetActive(false);
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            //Debug.Log($"Pointer down {index} item");
            UIBottomBar.Instance.SelectBottomBarItem(index);
        }
    }
}